<?php

namespace app\common\customer\model;


use app\common\config\logic\DictionaryLogic;
use app\common\config\logic\RegionLogic;
use app\common\config\model\ConfigStatus;
use app\common\product\logic\StoreCategoryLogic;
use app\common\system\logic\UserLogic;
use think\Model;
use think\model\concern\SoftDelete;

class CustomerBanquet extends Model
{
    protected $table = "tk_member_banquet";
    use SoftDelete;
    protected $defaultSoftDelete = 0;

    protected $type = [
        "zone" => "json",
        "store" => "json",
    ];

    public function getCategoryAttr($value, $data) {
        $categoryLogic = new StoreCategoryLogic();
        return $categoryLogic->getStoreCategory($data['category_id'], ['id', 'title', 'customer_model', 'order_model', 'on_member_company', 'is_sale']);
    }

    public function getCreatorAttr($value, $data)
    {
        $userLogic = new UserLogic();
        return $userLogic->getUser($data['create_id']);
    }

    public function getCityAttr($value, $data)
    {
        $logic = new RegionLogic();
        return $logic->getRegion($data['city_id'], ['id', 'shortname']);
    }

    public function getTagAttr($value, $data) {
        $dictionaryLogic = new DictionaryLogic();
        $row = $dictionaryLogic->getItem($data['status'], ConfigStatus::class, ['id','title']);

        return empty($row) ? "未跟进" : $row['title'];
    }

    public function setZoneIdsAttr($value, $data) {
        $zoneArr = json_decode($data['zone'], 1);
        if (empty($zoneArr)) {
            return '';
        } else {
            $ids = array_column($zoneArr, 'id');
            return join(',', $ids);
        }
    }   

    public function getZoneTextAttr($value, $data) {
        $zoneArr = json_decode($data['zone'], 1);
        if (empty($zoneArr)) {
            return '';
        } else {
            $shortNames = array_column($zoneArr, 'shortname');
            return join(',', $shortNames);
        }
    }
}